package org.jeecg.modules.demo.sermanager.service.impl;

import org.jeecg.modules.demo.sermanager.entity.NetptDetection;
import org.jeecg.modules.demo.sermanager.entity.NetptDetectionRet;
import org.jeecg.modules.demo.sermanager.mapper.NetptDetectionRetMapper;
import org.jeecg.modules.demo.sermanager.mapper.NetptDetectionMapper;
import org.jeecg.modules.demo.sermanager.service.INetptDetectionService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: netpt_detection
 * @Author: jeecg-boot
 * @Date:   2022-01-07
 * @Version: V1.0
 */
@Service
public class NetptDetectionServiceImpl extends ServiceImpl<NetptDetectionMapper, NetptDetection> implements INetptDetectionService {

	@Autowired
	private NetptDetectionMapper netptDetectionMapper;
	@Autowired
	private NetptDetectionRetMapper netptDetectionRetMapper;
	
	@Override
	@Transactional
	public void saveMain(NetptDetection netptDetection, List<NetptDetectionRet> netptDetectionRetList) {
		netptDetectionMapper.insert(netptDetection);
		if(netptDetectionRetList!=null && netptDetectionRetList.size()>0) {
			for(NetptDetectionRet entity:netptDetectionRetList) {
				//外键设置
				entity.setDetectionId(netptDetection.getId());
				netptDetectionRetMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void updateMain(NetptDetection netptDetection,List<NetptDetectionRet> netptDetectionRetList) {
		netptDetectionMapper.updateById(netptDetection);
		
		//1.先删除子表数据
		netptDetectionRetMapper.deleteByMainId(netptDetection.getId());
		
		//2.子表数据重新插入
		if(netptDetectionRetList!=null && netptDetectionRetList.size()>0) {
			for(NetptDetectionRet entity:netptDetectionRetList) {
				//外键设置
				entity.setDetectionId(netptDetection.getId());
				netptDetectionRetMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		netptDetectionRetMapper.deleteByMainId(id);
		netptDetectionMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			netptDetectionRetMapper.deleteByMainId(id.toString());
			netptDetectionMapper.deleteById(id);
		}
	}
	
}
